const dayjs = require('dayjs');
const {
    getCache,
    updateWeekPriceCache,
    updateLowPriceCache,
    updateHighPriceCache
} = require('../controllers/cachedController');
const {
    getWeekStockPrice,
    getHistoryStockPrice
} = require('../controllers/stockController');
const lowPriceMap = require('../config/lowPriceMap');


let cacheLength = 0;
async function getWeekPrice() {
    if (dayjs().minute() === 1) {
        cacheLength = 0;
    }

    let stocks = getCache();
    let stockCodes = [];
    if (stocks.length > 0) {
        stockCodes = stocks.map((item) => item.code);
    } else {
        stockCodes = Object.keys(lowPriceMap);
    }

    const weekPriceList = await getWeekStockPrice(stockCodes);

    let res = {};
    for (i = 0; i < weekPriceList.length; i++) {
        let weekPrice = weekPriceList[i];
        for (let code in weekPrice) {
            if (weekPrice.hasOwnProperty(code)) {
                res[code] = weekPrice[code];
            }
        }
    }

    console.log(`近五日数量：${Object.keys(res).length}，缓存长度：${cacheLength}`);
    if (Object.keys(res).length > 0 && Object.keys(res).length > cacheLength) {
        cacheLength = Object.keys(res).length;
        updateWeekPriceCache(res);
    }
}

async function calculateLowPrice() {
    if (dayjs().minute() === 1) {
        cacheLength = 0;
    }

    let stocks = getCache();
    let stockCodes = [];
    if (stocks.length > 0) {
        stockCodes = stocks.map((item) => item.code);
    } else {
        stockCodes = Object.keys(lowPriceMap);
    }

    const historyStockPriceList = await getHistoryStockPrice(stockCodes, 'low');

    let res = {};
    for (i = 0; i < historyStockPriceList.length; i++) {
        let historyStockPrice = historyStockPriceList[i];
        for (let code in historyStockPrice) {
            if (historyStockPrice.hasOwnProperty(code)) {
                let priceList = historyStockPrice[code];
                let sortItem = priceList.sort((a, b) => a - b);
                if (sortItem.length < 50) {
                    continue;
                }
                let low = sortItem[0];
                let minBuyPrice = sortItem[15];
                let maxBuyPrice = sortItem[30];

                res[code] = [low, minBuyPrice, maxBuyPrice];
            }
        }
    }

    console.log(`近百日最低价数量：${Object.keys(res).length}，缓存长度：${cacheLength}`);
    if (Object.keys(res).length > 0 && Object.keys(res).length > cacheLength) {
        cacheLength = Object.keys(res).length;
        updateLowPriceCache(res);
    }
}

async function calculateHighPrice() {
    if (dayjs().minute() === 25) {
        cacheLength = 0;
    }

    let stocks = getCache();
    let stockCodes = [];
    if (stocks.length > 0) {
        stockCodes = stocks.map((item) => item.code);
    } else {
        stockCodes = Object.keys(lowPriceMap);
    }
    const historyStockPriceList = await getHistoryStockPrice(stockCodes, 'high');

    let res = {};
    for (i = 0; i < historyStockPriceList.length; i++) {
        let historyStockPrice = historyStockPriceList[i];
        for (let code in historyStockPrice) {
            if (historyStockPrice.hasOwnProperty(code)) {
                let priceList = historyStockPrice[code];
                let sortItem = priceList.sort((a, b) => b - a);
                if (sortItem.length < 50) {
                    continue;
                }
                res[code] = [sortItem[30], sortItem[15], sortItem[0]];
            }
        }
    }

    console.log(`近百日最高价数量：${Object.keys(res).length}，缓存长度：${cacheLength}`);
    if (Object.keys(res).length > 0 && Object.keys(res).length > cacheLength) {
        cacheLength = Object.keys(res).length;
        updateHighPriceCache(res);
    }
}

module.exports = { getWeekPrice, calculateLowPrice, calculateHighPrice };
